from pyo import *
from pref import prefs

# audio slot control:
#     from OSC:
        # _load new effect
        # _ parameter
        # 
#     from program:
        #_ audio routing

class AudioSlot(object):
    """
    Class loaded into OscClass Slot acording to user selection

    this class is a parent to the actual audio process class
    """
    def __init__(self,slottype,name,ctrls):
        self.type = slottype #declare "mod" or "fx" type
        self.input = Sig([0 for i in range(prefs["channels"])])
        self.output = Sig([0 for i in range(prefs["channels"])])
        self.modOut = Sig(0)
        # ctrls are ctrl + mod already added
        self.ctrl1 = Sig(0)
        self.ctrl2 = Sig(0)
        self.ctrl3 = Sig(0)
        self.initVals= [0,0,0] # method gets these values and sends to lemur on init
        self.info = {"name":name,"ctrls":ctrls, "namesAndCtlrs":[ctrls[0],ctrls[1],ctrls[2], name]}
        self.onoff = []

    def addOff(self, obj):
        if type(obj)==type([0]):
            for i in obj:
                self.onoff.append(i)
        else:
            self.onoff.append(obj)

    def turnOff(self):
        for obj in self.onoff:
            obj.stop()

    def turnOn(self):
        for obj in self.onoff:
            obj.play()

    def setAudioIn(self, sig):
        self.input.setValue(sig)


    def setCtrls(self,ctrls):
        self.ctrl1.setValue(ctrls[0])
        self.ctrl2.setValue(ctrls[1])
        self.ctrl3.setValue(ctrls[2])

    def getAudioOut(self):
        return self.output

    def getMod(self):
        return self.modOut

    def getName(self):
        return self.info["name"]

    def getCtrlNames(self):
        return self.info["ctrls"]

    def getAllInfo(self):
        return self.info["namesAndCtlrs"]

    def getInitVals(self):
        return self.initVals


# class Filter(AudioSlot):
    
#   """
#   example audio process
#   """
#   name = "LowPass filter"
#   ctrls = ["freq","Q","gain"]
#   def __init__(self):
#       AudioSlot.__init__(self, "fx",self.name,self.ctrls)
#       self.output = Biquad(
#                   input=self.input, 
#                   freq=(self.ctrl1**2)*10000+20, 
#                   q=self.ctrl2*100+1, 
#                   mul = self.ctrl3**2)
#       self.addOff(self.output)
#       self.initVals = [1,0,1]


# class Verby(AudioSlot):
    
#   """
#   example audio process
#   """
#   name = "FreeVerb"
#   ctrls = ["size","damp","bal"]
#   def __init__(self):
#       AudioSlot.__init__(self, "fx",self.name,self.ctrls)
#       self.output = Freeverb(
#                   input=self.input, 
#                   size=self.ctrl1,
#                   damp=self.ctrl2,
#                   bal =self.ctrl3)
#       self.addOff(self.output)
#       self.initVals = [0.5,0.5,0.1]

# class AllPassFilter(AudioSlot):
#     """
#     example audio process
#     """
#     name = "AllPassFilter"
#     ctrls = ["freq","BandW","gain"]
#     def __init__(self):
#         AudioSlot.__init__(self, "fx", self.name,self.ctrls)
#         self.output = Allpass2(
#                      input=self.input, 
#                      freq=(self.ctrl1**2)*10000+20, 
#                      bw=self.ctrl2*100+1, 
#                      mul=self.ctrl3**2)
#         self.addOff(self.output)
#         self.initVals = [1,0,1]
        
# class Harmoniz(AudioSlot):
#     """
#     example audio process
#     """
#     name = "Harmonizer"
#     ctrls = ["transpo","feedback","gain"]
#     def __init__(self):
#         AudioSlot.__init__(self, "fx", self.name,self.ctrls)
#         self.output = Harmonizer(   
#                      input=self.input,
#                      transpo=(self.ctrl1)*12,
#                      feedback=self.ctrl2,
#                      mul=self.ctrl3**2) 
#         self.addOff(self.output)
#         self.initVals = [1,0.1,0.5]

# class Choruss(AudioSlot):
#     """
#     example audio process
#     """
#     name = "Chorus"
#     ctrls = ["depth","feedback","bal"]
#     def __init__(self):
#         AudioSlot.__init__(self, "fx", self.name,self.ctrls)
#         self.output = Chorus(   
#                      input=self.input,
#                      depth=self.ctrl1*5,
#                      feedback=self.ctrl2,
#                      bal=self.ctrl3) 
#         self.addOff(self.output)
#         self.initVals = [1,0.1,0.6]       

# class Distor(AudioSlot):
#     """
#     example audio process
#     """
#     name = "Distortion"
#     ctrls = ["drive","slope","mul"]
#     def __init__(self):
#         AudioSlot.__init__(self, "fx", self.name,self.ctrls)
#         self.output = Disto(   
#                      input=self.input,
#                      drive=self.ctrl1,
#                      slope=(self.ctrl2**2),
#                      mul=self.ctrl3**2) 
#         self.addOff(self.output)
#         self.initVals = [0.3,0.7,0.5]
        

        
# class AllpassWavG(AudioSlot):
#     """
#     example audio process
#     """
#     name = "AllpassWaveGuide"
#     ctrls = ["freq","feed","detune"]
#     def __init__(self):
#         AudioSlot.__init__(self, "fx", self.name,self.ctrls)
#         self.output = AllpassWG(   
#                      input=self.input,
#                      freq=(self.ctrl1**2)*1000+20,
#                      feed=self.ctrl2**2,
#                      detune=self.ctrl3) 
#         self.addOff(self.output)
#         self.initVals = [0.1,0.5,0.5]

# class WGVerby(AudioSlot):
    
#   """
#   example audio process
#   """
#   name = "WGReverb"
#   ctrls = ["feedback","cutoff","bal"]
#   def __init__(self):
#       AudioSlot.__init__(self, "fx",self.name,self.ctrls)
#       self.output = WGVerb(
#                   input=self.input, 
#                   feedback=self.ctrl1,
#                   cutoff=(self.ctrl1**2)*10000+20,
#                   bal=self.ctrl3)
#       self.addOff(self.output)
#       self.initVals = [0.5,0.4,0.4]  

# class Echo(AudioSlot):
    
#   """
#   example audio process
#   """
#   name = "Delay"
#   ctrls = ["delay","feedback","gain"]
#   def __init__(self):
#       AudioSlot.__init__(self, "fx",self.name,self.ctrls)
#       self.output = Delay(
#                   input=self.input, 
#                   delay=self.ctrl1,
#                   feedback=(self.ctrl1**2),
#                   mul=self.ctrl3**2)
#       self.addOff(self.output)
#       self.initVals = [0.2,0.1,0.5]


# class Decimator(AudioSlot):
    
#   """
#   example audio process
#   """
#   name = "Decimator"
#   ctrls = ["bitdepth","srscale","gain"]
#   def __init__(self):
#       AudioSlot.__init__(self, "fx",self.name,self.ctrls)
#       self.output = Degrade(
#                   input=self.input, 
#                   bitdepth=(self.ctrl1*32)+1,
#                   srscale=((self.ctrl2**2)*0.999)+0.001,
#                   mul=self.ctrl3**2)
#       self.addOff(self.output)
#       self.initVals = [0.5,1,0.5]

# class Lfo(AudioSlot):
#   """
#   example audio process
#   """
#   name = "Lfo"
#   ctrls = ["freq1","freq2","morph"]
#   def __init__(self):
#       AudioSlot.__init__(self, "mod",self.name,self.ctrls)
#       self.modOut = Sine(self.ctrl1*5+0.01, mul = self.ctrl3*0.5,add = .5)+Sine(self.ctrl2*5+0.1, mul = (1-self.ctrl3)*.5,add=.5)
#       self.initVals = [0,0,0.5]

# class EmptyFx(AudioSlot):
#   """
#   example audio process
#   """
#   name = "Empty"
#   ctrls = ["None","None","None"]
#   def __init__(self):
#       AudioSlot.__init__(self, "fx",self.name,self.ctrls)
#       self.output = self.input


# class EmptyMod(AudioSlot):
#   """
#   example audio process
#   """
#   name = "Empty"
#   ctrls = ["None","None","None"]
#   def __init__(self):
#       AudioSlot.__init__(self, "mod",self.name,self.ctrls)
        

# # PUT MODULES IN HERE!
# # Max 19 modules per category

# modulesDict = {"fx":
#                   {0:{"class":EmptyFx},
#                   1:{"class":Filter},
#                   2:{"class":Verby},
#                   3:{"class":AllPassFilter},
#                     4:{"class":Harmoniz},
#                     5:{"class":Choruss},
#                     6:{"class":Distor},
#                     7:{"class":AllpassWavG},
#                     8:{"class":WGVerby},
#                     9:{"class":Echo},
#                     10:{"class":Decimator}},
#               "mod":
#                   {0:{"class":EmptyMod},
#                   1:{"class": Lfo}
#                   }}


# # for i in range(16):
# #     modulesDict["fx"][i+3] = {"class":Filter} 


# for slotypename, classesDict in modulesDict.items():#autogenerate dict acording to slot info 
#   for i, slot in classesDict.items():
#       slot["name"] = slot["class"].name
#       slot["ctrls"] = slot["class"].ctrls

